<?php

/**
 * @description: 公众号入口转发
 * @param {type} 
 * @return: 
 * @Author: 偻儸小卒[EdisonLiu_]
 * @Date: 2020-05-20 11:07:05
 */

// ini_set('display_errors', 1);
// error_reporting(E_ALL);

defined('MODULE_ROOT') or define('MODULE_ROOT', dirname(__FILE__));

class We7_testModuleSite extends WeModuleSite
{

	private $route;
	private $container;
	public function __construct()
	{
		$container = include __DIR__ . '/bootstrap.php';
		$this->route = include __DIR__ . '/routes/app.php';
		$this->adminRoute = include __DIR__ . '/routes/admin.php';
		$this->container = $container;
	}

	public function __call($name, $arguments)
	{

		ob_clean(); //调试模式下 清除外部框架 的错误输出 要不会报错
		$container  = $this->container;
		/**
		 * @var $request \Psr\Http\Message\ServerRequestInterface
		 */
		$name = strtolower($name);

		//判断是否访问后台方法
		$do_type=0;
		if (strpos($name, 'doweb') === 0) {
			$do_type=1;
		} 

		//下面这句是去掉问时前面的 domobile 和  doweb
		$name = preg_replace('/domobile|doweb/', '', $name);


		$request = $container->get('request');
		$query = $request->getQueryParams();
		$request = $request->withUri(new \Zend\Diactoros\Uri('/' . $name))->withQueryParams($query);
		//如果是后台访问(doWeb) [注意：小程序后台点击记录也是跳转到这个路由中] 则路由到admin 否则路由到公众号前端 
		
		if ($do_type==1) {
			// 把do 操作转成rest  风格
			$response = $this->adminRoute->dispatch($request, $container->get('response'));
		} else{
			// 把do 操作转成rest  风格
			$response = $this->route->dispatch($request, $container->get('response'));
		}

		$container->get('emitter')->emit($response);
	}
}
